/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UTsHastaCambioObjSist.UT14_EventoDeCursada;

import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author Nahuel
 */
public class UT14_2_EventosDeCursada extends javax.swing.JFrame {

    public class Evento{
        private String tipo;
        private String fecha;
        private String alumno;
        private boolean seleccionado;

        public Evento(String tipo, String fecha, String alumno, boolean seleccionado) {
            this.tipo = tipo;
            this.fecha = fecha;
            this.alumno = alumno;
            this.seleccionado = seleccionado;
        }

        public String getAlumno() {
            return alumno;
        }

        public void setAlumno(String alumno) {
            this.alumno = alumno;
        }

        public String getFecha() {
            return fecha;
        }

        public void setFecha(String fecha) {
            this.fecha = fecha;
        }

        public boolean isSeleccionado() {
            return seleccionado;
        }

        public void setSeleccionado(boolean seleccionado) {
            this.seleccionado = seleccionado;
        }

        public String getTipo() {
            return tipo;
        }

        public void setTipo(String tipo) {
            this.tipo = tipo;
        }
        
        
    }
    
    public class ModeloTabla extends AbstractTableModel {
    private List<Evento> lista = new ArrayList();
    private Class[] types = new Class []{
        java.lang.String.class, java.lang.String.class, java.lang.String.class, JRadioButton.class
    };
  
    
    public void cargarLista(List<Evento> lista){
        this.lista= lista;
        for (int i = 0; i < lista.size(); i++) {
            this.setValueAt(lista.get(i).getTipo(), i, 0);
            this.setValueAt(lista.get(i).getFecha(), i, 1);
            this.setValueAt(lista.get(i).getAlumno(), i, 2);
            this.setValueAt(lista.get(i).isSeleccionado(), i, 3);
        }
        this.fireTableDataChanged();
    }
    
//    public List<Object> getSeleccionados() {
//        List<Object> listaSeleccionados = new ArrayList<CiiuDTO>();
//        
//        for (CiiuDTO ciiuDTO : lista) {
//            if(ciiuDTO.isSeleccionada()) listaSeleccionados.add(ciiuDTO);
//        }
//        return listaSeleccionados;
//    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return this.types[columnIndex];
    }

    @Override
    public String getColumnName(int column) {
        switch(column){
            case 0:
                return "Tipo de Evento";
            case 1:
                return "Fecha";
            case 2:
                return "Alumno";
            case 3:
                return "Seleccionado";
            default:
                throw new RuntimeException("columna no existe");
        }
    
    }
    
        @Override
    public int getRowCount() {
        return lista.size();
    }

        @Override
    public int getColumnCount() {
        return 4;
    }

        @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch(columnIndex){
            case 0:
                return lista.get(rowIndex).getTipo();
            case 1:
                return lista.get(rowIndex).getFecha();
            case 2:
                return lista.get(rowIndex).getAlumno();
            case 3:
                return new JRadioButton("",lista.get(rowIndex).isSeleccionado());
            default:
                throw new RuntimeException("columna no existe");
        }
    }
    public void tableChanged(TableModelEvent e) {
        this.fireTableChanged(e);
        repaint();
    }
    }
    

    
    class RadioButtonEditor extends DefaultCellEditor implements ItemListener {
  private JRadioButton button;

  public RadioButtonEditor(JCheckBox checkBox) {
    super(checkBox);
  }

        @Override
  public Component getTableCellEditorComponent(JTable table, Object value,
      boolean isSelected, int row, int column) {
    if (value == null)
      return null;
    button = (JRadioButton) value;
    button.addItemListener(this);
    button.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
    return (Component) value;
  }

        @Override
  public Object getCellEditorValue() {
    button.removeItemListener(this);
    return button;
  }

        @Override
  public void itemStateChanged(ItemEvent e) {
    super.fireEditingStopped();
  }
}
class RadioButtonRenderer implements TableCellRenderer {
        @Override
  public Component getTableCellRendererComponent(JTable table, Object value,
      boolean isSelected, boolean hasFocus, int row, int column) {
    if (value == null)
      return null;
    return (Component) value;
  }
}
    



    /**
     * Creates new form UT14_2_EventosDeCursada
     */
    public UT14_2_EventosDeCursada() {
        initComponents();
        UIDefaults ui = UIManager.getLookAndFeel().getDefaults();
        UIManager.put("RadioButton.focus", ui.getColor("control"));

        ModeloTabla modelo = new ModeloTabla();
        List<Evento> lista = new ArrayList();
        lista.add(new Evento("Examen", "12-03-2012", "Ivan Pianetti", false));
        lista.add(new Evento("Examen", "16-05-2012", "Alejandro Dus", false));
        lista.add(new Evento("Examen", "21-03-2012", "Matías Borges", true));
        lista.add(new Evento("Trabajo Práctico", "03-04-2012", "Nahuel Grasso", false));
        modelo.cargarLista(lista);
    
        jTable1.setModel(modelo);

        ButtonGroup group1 = new ButtonGroup();
        group1.add((JRadioButton) modelo.getValueAt(0, 3));
        group1.add((JRadioButton) modelo.getValueAt(1, 3));
        group1.add((JRadioButton) modelo.getValueAt(2, 3));
        group1.add((JRadioButton) modelo.getValueAt(3, 3));
        
        jTable1.getColumn("Seleccionado").setCellRenderer(
            new RadioButtonRenderer());
        jTable1.getColumn("Seleccionado").setCellEditor(
            new RadioButtonEditor(new JCheckBox()));
        setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));

        jLabel1.setText("Filtros auxiliares del tipo de evento");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(136, 136, 136)
                .addComponent(jLabel1)
                .addContainerGap(171, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jLabel1)
                .addContainerGap(38, Short.MAX_VALUE))
        );

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Tipo de Evento", "Fecha", "Alumno", "Seleccionar"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Ver Detalles");

        jMenu1.setText("Eventos de Cursada");
        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(31, 31, 31))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(178, 178, 178)
                        .addComponent(jButton1)))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        
        
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(UT14_2_EventosDeCursada.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(UT14_2_EventosDeCursada.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(UT14_2_EventosDeCursada.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(UT14_2_EventosDeCursada.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new UT14_2_EventosDeCursada().setVisible(true);
                
            }
        });
        
    }
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
